

package views;

import DAO.AulaDao;
import DAO.CursoDao;
import views.panels.PanelGeneral;
import java.awt.BorderLayout;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import models.Aula;
import models.Curso;
/**
 *
 * @author sebastian
 */
public class InterfazAsignarAula extends javax.swing.JFrame {

    /** Creates new form InterfazAsignarAula */
    public InterfazAsignarAula() {
        initComponents();
         PanelGeneral panel=new PanelGeneral();
        this.add(panel,BorderLayout.SOUTH);
        inicializarTabla();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();

        setResizable(false);

        jLabel1.setText("Realizar una nueva asignación:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Curso", "Aula Asignada"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));

        jLabel4.setText("Escoja un aula:");

        jLabel3.setText("Escoja un curso:");

        jLabel2.setText("Asignaciones Actuales:");

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButton1.setText("Realizar Asignación");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButton2.setText("Eliminar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButton3.setText("Aceptar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel8.setText("Escoger un curso para eliminar la asignación:");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));

        jLabel7.setText("Eliminar Asignación:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(167, 167, 167)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel7)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(165, 165, 165)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(35, 35, 35)
                .addComponent(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jComboBox1.getSelectedIndex() <= 0){
            JOptionPane.showMessageDialog(null, "Debe seleccionar un curso", null, JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (jComboBox2.getSelectedIndex() <= 0){
            JOptionPane.showMessageDialog(null, "Debe seleccionar un aula", null, JOptionPane.ERROR_MESSAGE);
            return;
        }
        CursoDao cursoDao = new CursoDao();
        AulaDao aulaDao = new AulaDao();
        Curso curso = cursoDao.encontrarCursoPorNombre((String)jComboBox1.getSelectedItem());
        Aula aula = aulaDao.encontrarAula((String)jComboBox2.getSelectedItem());
        curso.setAulaId(aula);
        cursoDao.actualizarCurso(curso);
        actualizarTabla(curso);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (jComboBox3.getSelectedIndex() <= 0){
            JOptionPane.showMessageDialog(null, "Debe seleccionar un curso", null, JOptionPane.ERROR_MESSAGE);
            return;
        }
        CursoDao cursoDao = new CursoDao();
        Curso curso = cursoDao.encontrarCursoPorNombre((String)jComboBox3.getSelectedItem());
        curso.setAulaId(null);
        cursoDao.actualizarCurso(curso);
        actualizarTabla(curso);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfazAsignarAula().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private void inicializarTabla() {
        CursoDao cursoDao = new CursoDao();
        Collection cursos = cursoDao.encontrarCursos();
        AulaDao aulaDao = new AulaDao();
        List aulas = aulaDao.encontrarAulas();
        int fila = 0;
        for (Iterator it = cursos.iterator(); it.hasNext();) {
            Curso curso = (Curso) it.next();
            if (curso.getAulaId() != null){
                Aula aula = curso.getAulaId();
                if (aulas.contains(aula)){
                    aulas.remove(aula);
                }
                jComboBox3.addItem(curso.getNombrecurso());
                jTable1.setValueAt(curso.getNombrecurso(), fila, 0);
                jTable1.setValueAt(aula.getNombreAula(), fila, 1);
                fila++;
            }
            else{
                jComboBox1.addItem(curso.getNombrecurso());
            }
        }
        for (Iterator it = aulas.iterator(); it.hasNext();) {
            Object aula = it.next();
            jComboBox2.addItem(aula);
        }
    }

    private void actualizarTabla(Curso curso) {
        int fila = 0;
        boolean encontrado = false;
        while (jTable1.getValueAt(fila, 0) != null){
            if (encontrado || jTable1.getValueAt(fila, 0).equals(curso.getNombrecurso())){
                encontrado = true;
                if (jTable1.getValueAt(fila + 1, 0) != null){
                    jTable1.setValueAt(jTable1.getValueAt(fila + 1, 0), fila, 0);
                    jTable1.setValueAt(jTable1.getValueAt(fila + 1, 1), fila, 1);
                }
                else{
                    jTable1.setValueAt(null, fila, 0);
                    jTable1.setValueAt(null, fila, 1);
                }
            }
            fila++;
        }
        if (!encontrado){
            jTable1.setValueAt(curso.getNombrecurso(), fila, 0);
            jTable1.setValueAt(curso.getAulaId(), fila, 1);
        }
    }
}
